<?php

namespace App\Admin\Controllers\goods;


use App\Common\statement\CommonStatement;
use App\Models\goods\GoodsCate;
use App\Models\user\Depart;
use App\Services\goods\GoodsService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Routing\Controller;

class GoodsCateController extends Controller
{
    use HasResourceActions;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '产品分类';

    public function index(Content $content)
    {
        return $content
            ->title("支部或科室部门")
            ->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_url('goods-cates'));
                    $form->text('name', __('Name'))->required();
                    $form->select('type', __('支部或科室'))->options(CommonStatement::organization_type)->required();
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
    }




    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {
        return Depart::tree(function (Tree $tree) {
            $tree->disableCreate();
            $tree->branch(function ($branch) {
                $src = $branch['icon'] ;
                $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";

                return "{$branch['id']} - {$branch['name']} $logo";

            });
        });
    }

    /**
     * Edit interface.
     *
     * @param string  $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title(trans('admin.menu'))
            ->description(trans('admin.edit'))
            ->row($this->form()->edit($id));
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Depart());

//        $grid->model()->orderByDesc('sort');
        $grid->column('id', __('Id'));
        $grid->column('name', __('Name'));
        $grid->column('type', __('Cate'))->display(function ($type){
            if ($type == 1){
                return "科室";
            }else{
                return "支部";
            }
        });

        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(GoodsCate::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('pid', __('Pid'));
        $show->field('icon', __('Icon'));
        $show->field('img', __('Img'));
        $show->field('is_top', __('Is top'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Depart());
        $form->text('name', __('Name'))->required();
        $form->select('type', __('科室或支部'))
            ->options(CommonStatement::organization_type)
            ->readonly();
        return $form;
    }
}
